extends StaticBody3D

signal toggle_inventory(external_inventory_owner)

@export var inventory_data: InventoryData
@export var inventory_id: String

func player_interact():
	toggle_inventory.emit(self)


func save_inventory_array():
	var array = []
	for i in inventory_data.slot_datas:
		if i is SlotData:
			var dict: Dictionary = {
				"quantity": i.quantity,
				"item_id": i.item_data.id
			}
			array.append(dict)
		else:
			array.append(null)
	return array

func load_inventory_array(array: Array, items: Array[ItemData]):
	var items_dict = {}
	for i in items:
		items_dict[i.id] = i
	for i in range(array.size()):
		if array[i] != null:
			var slot = SlotData.new()
			if items_dict.has(array[i]['item_id']):
				slot.item_data = items_dict[array[i]['item_id']]
			else:
				slot.item_data = items_dict["corrupt"]
			slot.quantity = array[i]['quantity']
			inventory_data.slot_datas[i] = slot
		else:
			inventory_data.slot_datas[i] = null

